
products =  [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
shop_car = []
shop_cost = []
while True:
    print('{:-^15}'.format(' 商品列表 '))
    id = 0
    for commodity,price in products:
        id += 1
        print(id,commodity,price,sep='\t')
    prompt='请选择所需商品编号(q键退出): '
    choice = input(prompt).strip().lower()
    if choice == 'q':
        print("谢谢光临! Bye.")
        break
    if choice not in '123456':
        print("无效的输入!")
        continue
    shop_car.append(products[int(choice)-1])
    while True:
        print('{:*^15}'.format(" 当前购物车列表 "))
        for i in range(len(shop_car)):
            commodity, price = shop_car[i]
            print(i+1, commodity, price, sep='\t')
        print('{:*^22}'.format(" END "))
        prompt='1.确认购买\n2.删除购物车商品\n3.返回上级(继续购买or退出)\n'
        while True:
            choice = input(prompt).strip().lower()
            if choice in '123':
                break
            print("无效的输入!")
        if choice == '1':
            shop_cost.extend(shop_car)
            shop_car.clear()
            print('{:#^15}'.format(" 已购清单列表 "))
            sum = 0
            for i in range(len(shop_cost)):
                commodity, price = shop_cost[i]
                sum += price
                print(i + 1, commodity, price, sep='\t')
            print('总计: ',sum,'元',sep='')
            print('{:#^22}'.format(" END "))
            shop_cost.clear()
            break
        elif choice == '2':
            print('{:*^15}'.format(" 当前购物车列表 "))
            for i in range(len(shop_car)):
                commodity, price = shop_car[i]
                print(i + 1, commodity, price, sep='\t')
            print('{:*^22}'.format(" END "))
            prompt = "请选择要移出的商品号码: "
            while True:
                choice = input(prompt).strip()
                if not choice.isdigit():
                    print("不合法的输入!")
                    continue
                if int(choice) < 1 or int(choice) > len(shop_car):
                    print("无效的号码!")
                    continue
                while True:
                    yn = input("是否真的要移出此商品(y or n): ").strip().lower()
                    if yn not in ['y','n','yes','no']:
                        print("请回答'y' or 'n'!")
                        continue
                    break
                if yn in ['y','yes']:
                    print("移出: ",shop_car.pop(int(choice)-1))
                    break
                break
        elif choice == '3':
            print('{:*^15}'.format(" 当前购物车列表 "))
            for i in range(len(shop_car)):
                commodity, price = shop_car[i]
                print(i + 1, commodity, price, sep='\t')
            print('{:*^22}'.format(" END "))
            break

# 写的很不错，如果最后能在打印下总清单那就更好了




